package ferp.android.engagement;

import android.app.*;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import ferp.android.R;
import ferp.android.activities.Entrance;
import ferp.android.activities.Reschedule;
import ferp.android.activities.preferences.Preferences;
import ferp.android.managers.ProfileManager;
import ferp.core.log.Log;
import ferp.core.player.Profile;

import java.util.Date;

/**
 * User: olegs
 * Date: 12/8/13 2:09 PM
 */
public class Reminder extends BroadcastReceiver
{
  public static final String ENGAGEMENT = "engagement";
  public static final String TAG = Log.TAG + "/A";

  public static void alarm(Activity activity)
  {
    Context context = activity.getApplicationContext();
    NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, Reminder.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

    // clear previously registered alarms and schedule the new one
    am.cancel(pi);
    nm.cancelAll();
    am.set(AlarmManager.RTC, System.currentTimeMillis() + PERIOD, pi);

    Log.debug(TAG, "Set alarm for " + new Date(System.currentTimeMillis() + PERIOD));
  }

  @Override
  public void onReceive(Context context, Intent intent)
  {
    Log.debug(TAG, "Alarm received");

    PendingIntent ci = PendingIntent.getActivity(context, 0, new Intent(context, Entrance.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK).putExtra(ENGAGEMENT, true), PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    PendingIntent di = PendingIntent.getActivity(context, 0, new Intent(context, Reschedule.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    Profile profile = ProfileManager.regular(context).getCurrentProfile();
    long games = profile == null ? 0 : profile.getStatistics().games.played;
    long score = profile == null ? 0 : profile.getStatistics().score[0];
    Resources r = Preferences.setLocale(context, profile);
    NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder nb = new NotificationCompat.Builder(context);
    Notification n = nb.setContentIntent(ci).setDeleteIntent(di).setSmallIcon(R.drawable.icon)
                       .setLargeIcon(BitmapFactory.decodeResource(r, R.drawable.icon)).setTicker(r.getString(R.string.app_name))
                       .setAutoCancel(true).setContentTitle(r.getString(R.string.reminder_title))
                       .setContentText(String.format(r.getString(R.string.reminder_text), r.getString(R.string.app_name), String.valueOf(games), String.valueOf(score))).build();

    // show notification
    nm.cancelAll();
    nm.notify(0, n);
  }

  private static final long PERIOD = 7 * 24 * 60 * 60 * 1000L; // one week
}
